import type { MockMethod } from 'vite-plugin-mock'
import { resultSuccess, resultError, getRequestToken } from './utils'
import type { LoginRequest, UserInfo } from '@/types/api/auth'

// 模拟用户数据
const users: UserInfo[] = [
  {
    id: '1',
    username: 'admin',
    nickname: '管理员',
    avatar: '',
    roles: ['admin'],
    permissions: ['*'],
    createTime: new Date().toISOString(),
    lastLoginTime: new Date().toISOString(),
    email: 'admin@example.com',
    phone: '13800138000',
    deptId: '1',
    deptName: '技术部',
    bio: '系统管理员'
  },
  {
    id: '2',
    username: 'test',
    nickname: '测试用户',
    avatar: '',
    roles: ['test'],
    permissions: [
      'system:user:list', 
      'system:role:list', 
      'system:menu:list', 
      'system:dept:list',
      'config:dict:list',
      'config:setting:list',
      'file:list'
    ],
    createTime: new Date().toISOString(),
    lastLoginTime: new Date().toISOString(),
    email: 'test@example.com',
    phone: '13800138001',
    deptId: '2',
    deptName: '测试部',
    bio: '测试用户'
  }
]

export default [
  // 用户登录
  {
    url: '/api/auth/login',
    method: 'post',
    response: ({ body }: { body: LoginRequest }) => {
      const { username, password } = body
      const user = users.find(
        (item) => item.username === username
      )
      
      if (!user) {
        return resultError('用户名或密码错误')
      }
      
      return resultSuccess({
        token: `TOKEN_${username}_${Date.now()}`
      })
    }
  },
  
  // 获取用户信息
  {
    url: '/api/user/info',
    method: 'get',
    response: (request: { headers: Record<string, string> }) => {
      const token = getRequestToken(request)
      
      if (!token) return resultError('未登录', 401)
      
      const username = token.split('_')[1]
      const user = users.find((item) => item.username === username)
      
      if (!user) {
        return resultError('获取用户信息失败', 401)
      }
      
      return resultSuccess(user)
    }
  },
  
  // 用户退出登录
  {
    url: '/api/auth/logout',
    method: 'post',
    response: () => {
      return resultSuccess(null)
    }
  }
] as MockMethod[] 